package com.yun.controller;

import com.yun.pojo.ShareFile;
import com.yun.result.Result;
import com.yun.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class ShareController {
	@Autowired
	private ShareService shareService;

	/**
	 * 链接文件分享页面
	 * @param request
	 * @param shareUrl
	 * @return
	 */
	@RequestMapping("/share")
	public String share(HttpServletRequest request, String shareUrl) {
		try {
			List<ShareFile> files = shareService.findShare(request, shareUrl);    /**调用findShare方法，返回查询的分享文件*/
			request.setAttribute("files", files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "share";                   /**通过ajax显示到页面上*/
	}

	/**
	 * 按状态查找分享
	 *
	 * @param request
	 * @param status  状态 1|2|-1
	 * @return
	 */
	/**添加searchShare()方法，用于处理查询该用户已分享文件的请求*/
	@RequestMapping("/searchShare")                                /**1为公共分享 2为私密分享 -1为删除分享*/
	public @ResponseBody                                            /**将java对象转化为json格式的数据*/
	Result<List<ShareFile>> searchShare(HttpServletRequest request, int status) {
		try {
			List<ShareFile> files = shareService.findShareByName(request, status);    /**返回查询出来的分享文件集合*/
			Result<List<ShareFile>> result = new Result<>(415, true, "获取成功");
			result.setData(files);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result<>(411, false, "获取失败");
		}
	}

	/**
	 * 分享文件
	 *
	 * @param request
	 * @param currentPath 当前路径
	 * @param shareFile   分享文件名
	 * @return
	 */
	@RequestMapping("/shareFile")
	public @ResponseBody
	Result<String> shareFile(HttpServletRequest request, String currentPath, String[] shareFile) {
		try {
			String shareUrl = shareService.shareFile(request, currentPath, shareFile);
			Result<String> result = new Result<>(405, true, "分享成功");
			result.setData(shareUrl);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result<>(401, false, "分享失败");
		}
	}

	/**
	 * 取消分享
	 *
	 * @param url      链接url
	 * @param filePath 文件路径
	 * @param status   状态
	 * @return
	 */
	@RequestMapping("/cancelShare")
	public @ResponseBody
	Result<String> cancelShare(String url, String filePath, int status) {
		try {
			String msg = shareService.cancelShare(url, filePath, status);
			Result<String> result = new Result<>(425, true, msg);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result<>(421, false, "取消失败");
		}
	}
}
